/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.poker.handler;

import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.game.database.Database;
import extension.game.poker.PokerFlag;
import extension.game.poker.PokerGame;
import extension.game.poker.PokerPlayer;
import extension.game.poker.PokerRoomExtension;
import extension.poker.zone.Flag;
import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public class JoinGameHandler extends BaseClientRequestHandler {
    
    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        PokerRoomExtension pokerRoomExtension = (PokerRoomExtension) getParentExtension();
        //get data
        int chairId = params.getInt(PokerFlag.CHAIR_ID);
        long moneyJoin = 0;
        try {
            moneyJoin = params.getInt(PokerFlag.BUY_INTO_GAME);
        } catch (Exception ex) {
            moneyJoin = params.getLong(PokerFlag.BUY_INTO_GAME);
        }
        
        Debug.d(user.getName() + "-" + PokerFlag.CHAIR_ID + chairId + "," + PokerFlag.BUY_INTO_GAME + " :" + moneyJoin);
        //create new poker player
        Long totalMoney = (Long) user.getProperty(PokerFlag.PLAYER_MONEY);
        if (!Debug.isDebug) {
            if (totalMoney != null && totalMoney >= moneyJoin) {
                Database database = pokerRoomExtension.getDatabase();
                long oldMoneyJoin = database.getMoneyJoin("R", user.getName());
                long newMoney = totalMoney - moneyJoin + oldMoneyJoin;
                database.insetMoneyQueue(user.getName(), newMoney, moneyJoin, "R");
                PokerPlayer pokerPlayer = new PokerPlayer(moneyJoin, user, newMoney);
                user.setProperty(PokerFlag.PLAYER_MONEY, newMoney);
                
                PokerGame pokerGame = pokerRoomExtension.getPokerGame();
                int result = pokerGame.addPlayer(pokerPlayer, chairId);
                //result =0: failed, =1: success,2 waiting
                pokerRoomExtension.getResponse().sendJoinGameInfo(chairId, user.getName(), moneyJoin, newMoney, result);
                if (result == 1) {
                    pokerRoomExtension.getResponse().sendMessageChat(user.getName() + " vào bàn", 0);
                }
                Debug.d(user.getName() + ":join game result :" + result);
                if (result > 0) {
                    sendUpdate(pokerRoomExtension);
                }
            }
        } else {
            PokerPlayer pokerPlayer = new PokerPlayer(moneyJoin, user, totalMoney - moneyJoin);
            PokerGame pokerGame = pokerRoomExtension.getPokerGame();
            int result = pokerGame.addPlayer(pokerPlayer, chairId);
            //result =0: failed, =1: success,2 waiting
            pokerRoomExtension.getResponse().sendJoinGameInfo(chairId, user.getName(), moneyJoin, totalMoney - moneyJoin, result);
            if (result == 1) {
                pokerRoomExtension.getResponse().sendMessageChat(user.getName() + " vào bàn", 0);
            }
            Debug.d(user.getName() + ":join game result :" + result);
            if (result > 0) {
                sendUpdate(pokerRoomExtension);
            }
        }
    }
    
    private void sendUpdate(PokerRoomExtension ext) {
        Room room = ext.getParentRoom();
        String groupName = room.getGroupId();
        Room lobbyRoom = ext.getParentZone().getRoomByName("The Lobby");
//        List<Room> listRooms = ext.getParentZone().getRoomListFromGroup(groupName);
        List<User> listSend = lobbyRoom.getUserList();
        for (User u : listSend) {
            String groupView = (String) u.getProperty(Flag.GROUP_NAME);
            if (groupView.equals(groupName)) {
                Debug.d("send update to :" + u.getName());
                ext.getResponse().sendUpdateTable(room.getName(), ext.getPokerGame().getNumberPlayers(), room.getGroupId(), u);
            }
        }
    }
}
